import json

from .getPublicData import *
from myApp.models import JobInfo

def getPageData():
    return list(educations.keys()),workExperience

def getBarData(defaultEducation,defaultWorkExperience):
    if defaultEducation == '不限' and defaultWorkExperience == '不限':
        jobs = JobInfo.objects.all()
    elif defaultEducation == '不限':
        jobs = JobInfo.objects.filter(workExperience=defaultWorkExperience)
    elif defaultWorkExperience == '不限':
        jobs = JobInfo.objects.filter(educational=defaultEducation)
    else:
        jobs = JobInfo.objects.filter(educational=defaultEducation, workExperience=defaultWorkExperience)

    jobsType = {}
    for j in jobs:
        if j.practice == 0:
            if jobsType.get(j.type, -1) == -1:
                jobsType[j.type] = [json.loads(j.salary)[1]]
            else:
                jobsType[j.type].append(json.loads(j.salary)[1])
    barData = {}
    for k,v in jobsType.items():
        if not barData.get(k,0):
            barData[k] = [0 for x in range(5)]
        for i in v:
            s = i /1000
            if s < 10:
                barData[k][0] += 1
            elif s < 20:
                barData[k][1] += 1
            elif s < 30:
                barData[k][2] += 1
            elif s < 40:
                barData[k][3] += 1
            else:
                barData[k][4] += 1
    legends = list(barData.keys())
    if len(legends) == 0:
        legends = None
    return salaryList, barData, legends

def averageFn(list):
    total = 0
    for i in list:
        total += i
    return round(total / len(list), 1)
def pieData():
    jobs = getAllJobs()
    jobsType = {}
    for j in jobs:
        if j.practice == 1:
            if jobsType.get(j.type, -1) == -1:
                jobsType[j.type] = [json.loads(j.salary)[1]]
            else:
                jobsType[j.type].append(json.loads(j.salary)[1])
    result = []
    for k,v in jobsType.items():
        result.append({
            'name': k,
            'value': averageFn(v)
        })
    return result

def getLoudouData():
    '''
    精确匹配：fieldname=value
        示例：salaryMonth=1000 表示 salaryMonth 等于 1000。
    大于：fieldname__gt=value
        示例：salaryMonth__gt=0 表示 salaryMonth 大于 0。
    小于：fieldname__lt=value
        示例：salaryMonth__lt=5000 表示 salaryMonth 小于 5000。
    大于或等于：fieldname__gte=value
        示例：salaryMonth__gte=1000 表示 salaryMonth 大于或等于 1000。
    小于或等于：fieldname__lte=value
        示例：salaryMonth__lte=5000 表示 salaryMonth 小于或等于 5000。
    不等于：fieldname__ne=value
        示例：salaryMonth__ne=0 表示 salaryMonth 不等于 0。
    包含（用于字符字段）：fieldname__contains=value
        示例：name__contains="Engineer" 表示名字中包含 "Engineer" 的记录。
    '''
    jobs = JobInfo.objects.filter(salaryMonth__gt=0)
    data = {}
    for j in jobs:
        x = str(j.salaryMonth) + '薪'
        if data.get(x,-1) == -1:
            data[x] = 1
        else:
            data[x] += 1
    result = []
    for k,v in data.items():
        result.append({
            'name': k,
            'value': v
        })
    return result